#include <stdio.h>
#include <stdlib.h>
#include "operate.h"
#include "computer.h"
#include "mode.h"

char chessbp[ROW][COLUMN];
char per1[ROW][COLUMN];
char per2[ROW][COLUMN];

int x;  //行坐标
int y;  //列坐标

int m;
int n;

char flg_row;
char flg_column;
char flg_diagonal;

int is_one;


char player_go(void)
{
    /************************ 玩家 ************************/
    system("clear");
    puts("\n");
    draw_chessbd(chessbp, ROW, COLUMN);
    putchar(10);

    if (is_one == 1)
        printf("computer: %x, %x\n", n, m);
    is_one = 0;

    printf("================ PLAYER I ================\n");
PLAYER1:    get_in(&x, &y);
    if (chessbp[x][y] != '+')  //输入的坐标已被占用 bug
    {
        printf("The position is occupied !\nPlease input again:\n");
        goto PLAYER1;
    }
    else
    {
        chessbp[x][y] = 'I';
        per1[x][y] = 'Y';
    }

    flg_row = yn_row(per1, ROW, COLUMN);
    flg_column = yn_column(per1, ROW, COLUMN);
    flg_diagonal = yn_diagonal(per1, ROW, COLUMN);
    
    if (flg_row == 'y' || flg_column == 'y' || flg_diagonal == 'y')
    {
        system("clear");
        puts("\n");
        draw_chessbd(chessbp, ROW, COLUMN);
        putchar(10);
        printf("================ PLAYER I ================\n");
        puts("     *******************************");
        puts("           *    YOU WIN !    *      ");
        puts("     *******************************\n");

        printf("输入 q 回到主界面，输入 c 继续下一局：");
        char ch;
        scanf("%c", &ch);
        getchar();

        while (ch != 'q' && ch != 'c')
        {
            printf("输入不正确，请重新输入: ");
            scanf("%c", &ch);
            getchar();
        }

        if (ch == 'q')
            return 'q';
        if (ch == 'c')
            return 'c';
    }

    return 'n';
}


char computer_go(void)
{
    /*********************** COMPUTER ***********************/
    do
    {
        AI(chessbp, &m, &n);
    } while (chessbp[n][m] != '+');

    chessbp[n][m] = 'C';
    per2[n][m] = 'Y';

    flg_row = yn_row(per2, ROW, COLUMN);
    flg_column = yn_column(per2, ROW, COLUMN);
    flg_diagonal = yn_diagonal(per2, ROW, COLUMN);
    if (flg_row == 'y' || flg_column == 'y' || flg_diagonal == 'y')
    {
        system("clear");
        puts("\n");
        draw_chessbd(chessbp, ROW, COLUMN);
        putchar(10);
        printf("================ PLAYER I ================\n");
        puts("     *******************************");
        puts("           *    YOU LOST !    *     ");
        puts("     *******************************");

        printf("输入 q 回到主界面，输入 c 继续下一局：");
        char ch;
        scanf("%c", &ch);
        getchar();

        while (ch != 'q' && ch != 'c')
        {
            printf("输入不正确，请重新输入: ");
            scanf("%c", &ch);
            getchar();
        }

        if (ch == 'q')
            return 'q';
        if (ch == 'c')
            return 'c';
    }  

    is_one = 1;

    return 'n';
}


char player1_go(void)
{
    /************************ 玩家 ************************/
    system("clear");
    puts("\n");
    draw_chessbd(chessbp, ROW, COLUMN);
    putchar(10);
    printf("================ PLAYER 1 ================\n");
PLAYER1:    get_in(&x, &y);
    if (chessbp[x][y] != '+')  //输入的坐标已被占用 bug
    {
        printf("The position is occupied !\nPlease input again:\n");
        goto PLAYER1;
    }
    else
    {
        chessbp[x][y] = '1';
        per1[x][y] = 'Y';
    }

    flg_row = yn_row(per1, ROW, COLUMN);
    flg_column = yn_column(per1, ROW, COLUMN);
    flg_diagonal = yn_diagonal(per1, ROW, COLUMN);
    
    if (flg_row == 'y' || flg_column == 'y' || flg_diagonal == 'y')
    {
        system("clear");
        puts("\n");
        draw_chessbd(chessbp, ROW, COLUMN);
        putchar(10);
        printf("================ PLAYER 1 ================\n");
        puts("     *******************************");
        puts("           *    YOU WIN !    *      ");
        puts("     *******************************\n");

        printf("输入 q 回到主界面，输入 c 继续下一局：");
        char ch;
        scanf("%c", &ch);
        getchar();

        while (ch != 'q' && ch != 'c')
        {
            printf("输入不正确，请重新输入: ");
            scanf("%c", &ch);
            getchar();
        }

        if (ch == 'q')
            return 'q';
        if (ch == 'c')
            return 'c';
    }

    return 'n';
}


char player2_go(void)
{
    /************************ 玩家 ************************/
    system("clear");
    puts("\n");
    draw_chessbd(chessbp, ROW, COLUMN);
    putchar(10);
    printf("================ PLAYER 2 ================\n");
PLAYER2:    get_in(&x, &y);
    if (chessbp[x][y] != '+')  //输入的坐标已被占用 bug
    {
        printf("The position is occupied !\nPlease input again:\n");
        goto PLAYER2;
    }
    else
    {
        chessbp[x][y] = '2';
        per2[x][y] = 'Y';
    }

    flg_row = yn_row(per2, ROW, COLUMN);
    flg_column = yn_column(per2, ROW, COLUMN);
    flg_diagonal = yn_diagonal(per2, ROW, COLUMN);
    
    if (flg_row == 'y' || flg_column == 'y' || flg_diagonal == 'y')
    {
        system("clear");
        puts("\n");
        draw_chessbd(chessbp, ROW, COLUMN);
        putchar(10);
        printf("================ PLAYER 2 ================\n");
        puts("     *******************************");
        puts("           *    YOU WIN !    *      ");
        puts("     *******************************\n");

        printf("输入 q 回到主界面，输入 c 继续下一局：");
        char ch;
        scanf("%c", &ch);
        getchar();

        while (ch != 'q' && ch != 'c')
        {
            printf("输入不正确，请重新输入: ");
            scanf("%c", &ch);
            getchar();
        }

        if (ch == 'q')
            return 'q';
        if (ch == 'c')
            return 'c';
    }

    return 'n';
}


void mode_mac(void)
{
LOOP:    twoarr_init(chessbp, ROW, COLUMN);
    twoarr_init(per1, ROW, COLUMN);
    twoarr_init(per2, ROW, COLUMN);

    while (1)
    {
        char ch;

        printf("输入 1 先下， 输入 2 后下： ");
        scanf("%c", &ch);
        getchar();
        while (ch != '1' && ch != '2')
        {
            printf("输入不正确，请重新输入： ");
            scanf("%c", &ch);
            getchar();
        }

        if (ch == '1')
        {
            while (1)
            {
                if (player_go() == 'q' || computer_go() == 'q')
                    break;
                if (player_go() == 'c' || computer_go() == 'c')
                    goto LOOP;
            }
            break;
        }
        if (ch = '2')
        {
            while (1)
            {
                if (computer_go() == 'q'|| player_go() == 'q')
                    break;
                if (computer_go() == 'c'|| player_go() == 'c')
                    goto LOOP;
            }
            break;
        }

    }
}


void mode_mam(void)
{
LOOP:    twoarr_init(chessbp, ROW, COLUMN);
    twoarr_init(per1, ROW, COLUMN);
    twoarr_init(per2, ROW, COLUMN);

    while (1)
    {
        if (player1_go() == 'q'|| player2_go() == 'q')
            break;
        if (player1_go() == 'c'|| player2_go() == 'c')
            goto LOOP;
    }
}


/************************ (C) COPYRIGHT HOUTY PRIVATE ********END OF FILE****/

